.global exit
.global brk
.global open
.global close
.global write
.global read

#param trans   p0:rdi, p1:rsi, p2:rdx, p3:rcx
#Platform          Return Value  Parameter Registers         Additional Parameters     Stack Alignment     Scratch Registers                           Preserved Registers                             Call List
#System V i386     eax, edx      none                        stack (right to left)     -                   eax, ecx, edx                               ebx, esi, edi, ebp, esp                         ebp
#System V X86_64   rax, rdx      rdi, rsi, rdx, rcx, r8, r9  stack (right to left)     16-byte at call3    rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11   rbx, rsp, rbp, r12, r13, r14, r15               rbp
#ARM               r0, r1        r0, r1, r2, r3              stack                     8 byte4             r0, r1, r2, r3, r12                         r4, r5, r6, r7, r8, r9, r10, r11, r13, r14      -

#ifdef M32
.equ __NR_read, 3
.equ __NR_write, 4
.equ __NR_open, 5
.equ __NR_close, 6
.equ __NR_lseek, 19
.equ __NR_brk, 45
.equ __NR_exit, 1

#void exit(int exit_code)
exit:
    push %ebp
    movl %esp, %ebp
    push %ebx
    
    movl $__NR_exit, %eax
    movl 8(%ebp), %ebx
    int $0x80
    
    pop %ebx
    leave
    ret

brk:
    push %ebp
    movl %esp, %ebp
    push %ebx
    
    movl $__NR_brk, %eax
    movl 8(%ebp), %ebx
    int $0x80
    
    pop %ebx
    leave
    ret

#bx:file path cx:flags  dx:mode
#int open(const char* path, int flags, int mode)
open:
    push %ebp
    movl %esp, %ebp
    push %ebx
    
    movl $__NR_open, %eax    
    movl 8(%ebp), %ebx
    movl 12(%ebp), %ecx
    movl 16(%ebp), %edx
    int $0x80

    pop %ebx
    leave
    ret

#int close(int fd)
close:
    push %ebp
    movl %esp, %ebp
    push %ebx
    
    movl $__NR_close, %eax    
    movl 8(%ebp), %ebx
    int $0x80

    pop %ebx
    leave
    ret

#ssize_t read(int fd, void* buf, size_t size)
read:
    push %ebp
    movl %esp, %ebp
    push %ebx
    
    movl $__NR_read, %eax
    movl 8(%ebp), %ebx
    movl 12(%ebp), %ecx
    movl 16(%ebp), %edx
    int $0x80

    pop %ebx
    leave
    ret

#ssize_t write(int fd, const void* buf, size_t size)
write:
    push %ebp
    movl %esp, %ebp
    push %ebx
    
    movl $__NR_write, %eax    
    movl 8(%ebp), %ebx
    movl 12(%ebp), %ecx
    movl 16(%ebp), %edx
    int $0x80

    pop %ebx
    leave
    ret
#else
.equ __NR_read, 0
.equ __NR_write, 1
.equ __NR_open, 2
.equ __NR_close, 3
.equ __NR_lseek, 8
.equ __NR_brk, 12
.equ __NR_exit, 60

#void exit(int exit_code)
exit:
    push %rbp
    movq %rsp, %rbp
    push %rbx
    
    movq $__NR_exit, %rax
    movq %rdi, %rbx
    syscall
    
    pop %rbx
    leave
    ret


brk:
    push %rbp
    movq %rsp, %rbp
    push %rbx
    
    movq $__NR_brk, %rax
    movq %rdi, %rbx
    syscall
    
    pop %rbx
    leave
    ret

#bx:file path cx:flags  dx:mode
#                        di          si        dx
#int open(const char* path, int flags, int mode)
open:
    push %rbp
    movq %rsp, %rbp
    push %rbx
    
    movq $__NR_open, %rax    
    movq %rdi, %rbx
    movq %rsi, %rcx
    movq %rdx, %rdx
    syscall
    
    pop %rbx
    leave
    ret

#int close(int fd)
close:
    push %rbp
    movq %rsp, %rbp
    push %rbx
    
    movq $__NR_close, %rax    
    movq %rdi, %rbx
    syscall
    
    pop %rbx
    leave
    ret

#                    di        si          dx
#ssize_t read(int fd, void* buf, size_t size)
read:
    push %rbp
    movq %rsp, %rbp
    push %rbx
    
    movq $__NR_read, %rax
    movq %rdi, %rbx
    movq %rsi, %rcx
    movq %rdx, %rdx
    syscall
    
    pop %rbx
    leave
    ret

#ssize_t write(int fd, const void* buf, size_t size)
write:
    push %rbp
    movq %rsp, %rbp
    push %rbx
    
    movq $__NR_write, %rax    
    movq %rdi, %rbx
    movq %rsi, %rcx
    movq %rdx, %rdx
    syscall

    pop %rbx
    leave
    ret
#endif

